/*
 ============================================================================
 Name        : timing_scheme_aud.c
 Author      : chenf
 Version     : 1.0
 Copyright   : loongsec
 Description : add,update,delete timing_scheme in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "cgi.h"
#include "cookie.h"

void url_encode(char *s){
	int i=0, len = strlen(s);
	for(i=0;i < len; i++){
		printf("%%%-2X", s[i] & 0x000000ff );
	}
}

int main(void) {
        sqlite3 *db = NULL;
        int  nrow=0,ncolumn=0;
        char **azResult = NULL;
        char *zErrMsg =0;
        char sql[1000];
        char *input;
        char *req_method;
        int  rc;

        if(isvalid_cookie()!=0){
            return 0;
        }

        rc = sqlite3_open(PATH_DATABASE,&db);

        printf("Content-type:text/json;charset=utf-8\n\n");

        if (rc) {
            fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
            sqlite3_close(db);
            return 0;

        } else {
            req_method = getenv("REQUEST_METHOD");

            FILE* fp = (FILE*) stdin;
            input = getcgidata(fp, req_method);

            //PARAM un = getParam(input, "username");
            //printf("Your find is %s = %s  \n", un.key, un.value );

            //printf("Your input is %s<br>  \n", input );
            PARAM method  = getParam(input, "method");

            PARAM timing_scheme_index   = getParam(input, "timingSchemeIndex");
            PARAM period_length         = getParam(input, "periodLength");
            PARAM phase_difference      = getParam(input, "phaseDifference");
            PARAM coordinate_phase      = getParam(input, "coordinatePhase");
            PARAM stage_timing_index    = getParam(input, "stageTimingIndex");

            char last_modified[]      = {"datetime(CURRENT_TIMESTAMP,'localtime')"};

            if( strcmp(method.value, "add") ==0 ){
                sprintf(sql, " INSERT INTO [tb_timing_scheme] (\
                    [timing_scheme_index], \
                    [period_length], \
                    [phase_difference],  \
                    [coordinate_phase], \
                    [stage_timing_index], \
                    [last_modified])    \
                    VALUES ('%s', '%s', '%s', '%s', '%s', %s);",
                      timing_scheme_index.value,
                            period_length.value,
                         phase_difference.value,
                         coordinate_phase.value,
                       stage_timing_index.value,
                    last_modified);

            } else if( strcmp(method.value, "update") ==0 ){
                sprintf(sql, " UPDATE [tb_timing_scheme]  \
                    set [period_length]         = '%s',\
                        [phase_difference]      = '%s',\
                        [coordinate_phase]      = '%s',\
                        [stage_timing_index]    = '%s',\
                        [last_modified]         = %s \
                    WHERE [timing_scheme_index] = '%s' ;",
                              period_length.value,
                           phase_difference.value,
                           coordinate_phase.value,
                         stage_timing_index.value,
                            last_modified,
                        timing_scheme_index.value);

            } else if( strcmp(method.value, "delete") ==0 ){
                sprintf(sql, " DELETE FROM [tb_timing_scheme]  \
                    WHERE [timing_scheme_index]         = '%s';",
                    timing_scheme_index.value);

            }
            //printf("SQL: %s", sql);

            int sr = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
            char *err_msg = (char*)sqlite3_errmsg(db);
            printf("{\"res\": %d , \"msg\": \"", sr);
            url_encode(err_msg);
            printf("\"}");

            sqlite3_close(db);
            return 0;

        }
        fflush(stdout);

    return EXIT_SUCCESS;
}



